package org.zero.orika.converter.mapper;

import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.impl.ConfigurableMapper;
import org.zero.orika.converter.AccountConverter;
import org.zero.orika.converter.ByteAndBoolConverter;
import org.zero.orika.converter.GenderConverter;
import org.zero.model.po.UserPO;
import org.zero.model.bo.UserBO;

/**
 * @author Zero (cnzeropro@qq.com)
 * @date 2021/9/1 15:52
 */
public class UserMapper extends ConfigurableMapper {
    @Override
    protected void configure(MapperFactory factory) {
        ConverterFactory converterFactory = factory.getConverterFactory();
        // 注册转换器
        // 性别转换器（双向：Byte-GenderEnum）
        converterFactory.registerConverter("genderConverter", new GenderConverter());
        // 账户转换器（双向：Long-AccountDTO）
        converterFactory.registerConverter("accountConverter", new AccountConverter());
        //
        converterFactory.registerConverter("byteAndBoolConverter", new ByteAndBoolConverter());

        // DO 与 DTO 互转
        factory.classMap(UserPO.class, UserBO.class)
                .field("name", "username")
                .fieldMap("gender")
                .converter("genderConverter")
                .add()
                .fieldMap("accountId", "account")
                .converter("accountConverter")
                .add()
                .fieldMap("deleted")
                .converter("byteAndBoolConverter")
                .add()
                .byDefault()
                .register();

        // DTO 与 VO 互转
    }
}
